package com.ts.animal.controller;

import com.ts.animal.domain.RfidSyncDataDO;
import com.ts.animal.service.RfidSyncDataService;
import com.ts.common.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 标签（动物状态）同步数据表
 *
 * @author bobby
 * @email bobby@126.com
 * @date 2022-09-02 18:27:26
 */

@Controller
@RequestMapping("/animal/rfidSyncData")
public class RfidSyncDataController {
    @Autowired
    private RfidSyncDataService rfidSyncDataService;

    @GetMapping()
    @RequiresPermissions("animal:rfidSyncData:rfidSyncData")
    String RfidSyncData() {
        return "animal/rfidSyncData/rfidSyncData";
    }

    @ResponseBody
    @RequestMapping("/list")
    @RequiresPermissions("animal:rfidSyncData:rfidSyncData")
    public Object list(@RequestParam Map<String, Object> params) {

        //导出 start
        if (StringUtils.isNotEmpty(UtilExportExcelPlus.getFileId(params))) {
            String startCollectionTimeStr = (String) params.get("startCollectionTime");
            String endCollectionTimeStr = (String) params.get("endCollectionTime");
            if (StringUtils.isAnyEmpty(startCollectionTimeStr, endCollectionTimeStr)) {
                return Result.error("采集时间不能为空");
            } else {
                Date startCollectionTime = UtilDate.convertDateTimeByFormate(startCollectionTimeStr, DateUtils.DATE_TIME_PATTERN);
                Date endCollectionTime = UtilDate.convertDateTimeByFormate(endCollectionTimeStr, DateUtils.DATE_TIME_PATTERN);
                long intervalDays = (endCollectionTime.getTime() - startCollectionTime.getTime()) / (1000 * 60 * 60 * 24);
                if (intervalDays > 2) {
                    return Result.error("采集时间跨度不能超过两天");
                }
            }
        }
        //查询列表数据
        PageUtils pageUtils = new PageInfos(params) {
            @Override
            public List inject(Map params) {
                List<Map<String, Object>> rfidSyncDataList = rfidSyncDataService.list(params);
                return rfidSyncDataList;
            }
        }.query().lowerUnderscore();
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("animal:rfidSyncData:add")
    String add() {
        return "animal/rfidSyncData/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("animal:rfidSyncData:edit")
    String edit(@PathVariable("id") Long id, Model model) {
        RfidSyncDataDO rfidSyncData = rfidSyncDataService.get(id);
        model.addAttribute("rfidSyncData", rfidSyncData);
        return "animal/rfidSyncData/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("animal:rfidSyncData:add")
    public Result save(RfidSyncDataDO rfidSyncData) {
        Result result = Result.ok();
        rfidSyncDataService.save(result, rfidSyncData);
        return result;
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("animal:rfidSyncData:edit")
    public Result update(RfidSyncDataDO rfidSyncData) {
        Result result = Result.ok();
        rfidSyncDataService.update(result, rfidSyncData);
        return result;
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("animal:rfidSyncData:remove")
    public Result remove(Long id) {
        Result result = Result.ok();
        rfidSyncDataService.remove(result, id);
        return result;
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("animal:rfidSyncData:batchRemove")
    public Result remove(@RequestParam("ids[]") Long[] ids) {
        Result result = Result.ok();
        rfidSyncDataService.batchRemove(result, ids);
        return result;
    }


    /**
     * 体温走势
     */
    @PostMapping("/temperature/{num}")
    @ResponseBody
    public Result temperatureChanges(@PathVariable("num") String num) {
        Result result = Result.ok();
        rfidSyncDataService.temperatureChanges(num);
        return result.setData(rfidSyncDataService.temperatureChanges(num));
    }

}
